#!/bin/sh

PROC_ID=`pidof webproc`
if [ -z "$PROC_ID" ]
then
        echo "webproc: No proccess to kill"
else
        while [ "x$PROC_ID" != "x" ]
        do
                kill -9 $PROC_ID
                sleep 1
                PROC_ID=`pidof webproc`
        done
fi

PROC_ID=`pidof dnsmasq`
if [ -z "$PROC_ID" ]
then
        echo "dnsmasq: No proccess to kill"
else
        while [ "x$PROC_ID" != "x" ]
        do
                kill -9 $PROC_ID
                sleep 1
                PROC_ID=`pidof dnsmasq`
        done
fi


ROOT_DIR="/var"
CONF_DIR="$ROOT_DIR/dnsmasq.conf.d"
HOSTS_DIR="$ROOT_DIR/addn_hosts.d"
if [ ! -d $ROOT_DIR ]; then `mkdir -p $ROOT_DIR`; fi
if [ ! -d $CONF_DIR ]; then `mkdir -p $CONF_DIR`; fi
if [ ! -d $HOSTS_DIR ]; then `mkdir -p $HOSTS_DIR`; fi

nohup $ROOT_DIR/webproc \
        -c $ROOT_DIR/dnsmasq.conf \
        -c $ROOT_DIR/addn_hosts \
        -m 100 \
        -- ./dnsmasq \
                --no-daemon \
                --log-queries \
                --user=root \
                --conf-file=$ROOT_DIR/dnsmasq.conf \
                --conf-dir=$CONF_DIR \
                --addn-hosts=$ROOT_DIR/addn_hosts \
                --addn-hosts=$HOSTS_DIR \
        >/dev/null 2>&1 &


# curl https://185.199.108.133/privacy-protection-tools/anti-AD/master/adblock-for-dnsmasq.conf --insecure -o /var/dnsmasq.conf.d/adblock-for-dnsmasq.conf
